package com.honglv.controller.common;

import com.honglv.context.BaseContext;
import com.honglv.exception.BaseException;

import com.honglv.pojo.dto.PageByCityDTO;
import com.honglv.pojo.dto.GetFoodDTO;
import com.honglv.pojo.dto.PageDTO;
import com.honglv.pojo.dto.UpdateFoodDTO;
import com.honglv.pojo.dto.UpdateHotelDTO;
import com.honglv.pojo.result.Result;
import com.honglv.pojo.vo.GetAllFoodsVO;
import com.honglv.pojo.vo.GetFoodsByCityVO;
import com.honglv.pojo.vo.PageVO;
import com.honglv.service.FoodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
@Api("美食相关接口")
@RequestMapping("/food")
public class FoodController {
    @Autowired
    private FoodService foodService;

    /**
     * 根据城市分页获取美食
     * @param pageByCityDTO
     * @return
     */
    @GetMapping("/getFoodsByCity")
    @ApiOperation("根据城市分页获取美食")
    public Result<PageVO<GetFoodsByCityVO>> getFoodsByCity(PageByCityDTO pageByCityDTO){
        if (pageByCityDTO == null || pageByCityDTO.getPageSize() == null || pageByCityDTO.getPage() == null){
            throw new BaseException("参数为空");
        }
        PageVO<GetFoodsByCityVO> pageVO = foodService.getFoodsByCity(pageByCityDTO);
        if (pageVO == null){
            return Result.success("暂无美食数据");
        }
        return Result.success(pageVO);
    }

    @GetMapping("/getFoodsByAuthorId")
    @ApiOperation("通过用户id获取美食")
    public Result<List<GetAllFoodsVO>> getFoodsByAuthorId(){
        Long userId = BaseContext.getCurrentId();
        if (userId == null){
            throw new BaseException("用户未登录");
        }
        List<GetAllFoodsVO> list = foodService.getFoodsByAuthorId(userId);
        if (list == null){
            return Result.success("用户未写作美食");
        }
        return Result.success(list);
    }

    @GetMapping("/deleteFood/{id}")
    @ApiOperation("删除美食")
    public Result deleteFood(@PathVariable Long id){
        return foodService.deleteFood(id);
    }

    @GetMapping("/updateFood")
    @ApiOperation("修改美食店信息")
    public Result updateFood(UpdateFoodDTO updateFoodDTO){
        return foodService.updateFood(updateFoodDTO);
    }
}
